package nju.bean.meeting.ServerManagement;

import nju.bean.meeting.database.DatabaseManager;
import nju.bean.meeting.database.DatabaseManagerInterface;
import nju.bean.meeting.log.LogMaker;
import nju.bean.meeting.meetingManagement.MainMeetingManager;
import nju.bean.meeting.net.ServerDidirectionalBearer;
import nju.bean.meeting.net.ServerNet;
import nju.bean.meeting.transporter.Feedback.Feedback;
import nju.bean.meeting.transporter.Feedback.JudgeFeedback;
import nju.bean.meeting.transporter.Request.Hello;
import nju.bean.meeting.transporter.Request.Request;

public class ServerManager {

	private int port;
	private int signalPort;
	private ServerNet inforBearer;
	private ServerNet signalBearer;
	private MainMeetingManager mmm;
	private UserRecorder ur;
	
	public ServerManager(int port,int signalPort){
		this.port = port;
		this.signalPort = signalPort;
		mmm = MainMeetingManager.getInstance();
		LogMaker.printMessage("Server start to work");
		ur = UserRecorder.getInstance();
		new Thread(ur).start();
		initializeNet();
	}
	
	private void initializeNet(){
		inforBearer = new MainBearer();
		inforBearer.start(port);
		signalBearer = new SignalBearer();
		signalBearer.start(signalPort);
	}
	
	private Feedback dealRequest(Request re){
		return re.deal();
	}
	
	private Feedback dealHello(Request request){
		Hello hello = (Hello)request;
		ur.deal(hello.getUserName());
		return new JudgeFeedback(true);
	}
	
	class MainBearer extends ServerDidirectionalBearer{

		@Override
		public Feedback dealCommand(Request request) {
			// TODO Auto-generated method stub
			return dealRequest(request);
		}

		@Override
		public void dealException(Exception e, int num) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	class SignalBearer extends ServerDidirectionalBearer{

		@Override
		public Feedback dealCommand(Request request) {
			// TODO Auto-generated method stub
			return dealHello(request);
		}

		@Override
		public void dealException(Exception e, int num) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public static void main(String arges[]){
		new ServerManager(8888,8889);
	}
}
